package com.qinyu.treehole.controller;

import com.qinyu.treehole.annotation.UserAuth;
import com.qinyu.treehole.entities.Feed;
import com.qinyu.treehole.entities.R;
import com.qinyu.treehole.service.FeedService;
import com.qinyu.treehole.service.ForwardService;
import com.qinyu.treehole.tool.FeedTool;
import com.qinyu.treehole.tool.ForwardTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/forward")
@Slf4j
@Validated
public class ForwardController {

    @Autowired
    private ForwardService forwardService;
    @Autowired
    private FeedService feedService;

    @UserAuth
    @PostMapping
    public R addForward(@RequestParam("userId")Integer userId,
                        @RequestParam("feedId")Integer feedId){
        log.info("用户id:{}尝试转发动态id:{}", userId, feedId);
        Feed feed = feedService.getById(feedId);
        if(feed == null){
            return R.no(null, FeedTool.FEED_NO_EXIST);
        }
        boolean f = forwardService.saveForward(userId, feedId);

        if(f) {
            return R.ok(null, ForwardTool.USER_FORWARD_SUCCESS);
        }

        return R.no(null, ForwardTool.USER_FORWARD_FAIL);
    }

}
